package org.nf.box.service.impl;



import jakarta.security.auth.message.AuthException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nf.box.common.jwt.JwtUtils;
import org.nf.box.domain.User;
import org.nf.box.mapper.UserMapper;
import org.nf.box.service.AuthService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lzp
 * @date 2024/12/23
 */
@Service("authService")
@RequiredArgsConstructor
@Transactional(rollbackFor = RuntimeException.class)
@Slf4j
public class AuthServiceImpl implements AuthService {

    private final UserMapper userMapper;

    @Override
    public String auth(String account, String password) {
        User user = userMapper.getUserByAccount(account);
        if (user.getAccountNumber() != null && user.getPassword() != null && user.getPassword().equals(password)) {
            Map<String, Object> map = new HashMap<>();
            map.put("user", user.getUserId());
            long ttl = 1000 * 60 * 60 * 24 * 7;
            //颁发token
            return JwtUtils.createToken(map, ttl);
        }
        throw new RuntimeException("账号或密码错误");
    }
}